package com.dier.webservices.restful.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dier.webservices.restful.model.Film;
import com.dier.webservices.restful.model.FilmList;
import com.dier.webservices.restful.service.ISFilmService;
import com.dier.webservices.restful.util.LoadData;

@Service
public class FilmServiceImpl implements ISFilmService{

	@Autowired
	private LoadData dataFilms;
	
	public Film searchFilm(Integer id) {
		for (Film film : dataFilms.getListOfFilms()) {
			if (id.equals(film.getId())) 
				return film;
		}
		return null;
	}
	
	public Film searchFilm(String name) {
		for (Film film : dataFilms.getListOfFilms()) {
			if (film.getNombre().indexOf(name) != -1) 
				return film;
		}
		return null;
	}

	public FilmList searchAllFilm() {
		return new FilmList(dataFilms.getListOfFilms());
	}

	public Boolean saveOrUpdateFilm(Film newFilm) {
		deleteFilm(newFilm.getId());
		return dataFilms.addNewFilm(newFilm);
	}

	public Boolean deleteFilm(Integer idDeleteFilm) {
		return dataFilms.deleteFilm(searchFilm(idDeleteFilm));
	}

	public LoadData getDataFilms() {
		return dataFilms;
	}

	public void setDataFilms(LoadData dataFilms) {
		this.dataFilms = dataFilms;
	}
}
